//
//  FMDBTool.m
//  LogisticsFollow
//
//  Created by JustFei on 2016/12/26.
//  Copyright © 2016年 manridy. All rights reserved.
//

#import "FMDBTool.h"

@implementation FMDBTool

static FMDatabase *_fmdb;

#pragma mark - init
/**
 *  创建数据库文件
 *
 *  @param path 数据库名字，以用户名+MotionData命名
 *
 */
- (instancetype)initWithPath:(NSString *)path
{
    self = [super init];
    
    if (self) {
        NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.sqlite",path]];
        _fmdb = [FMDatabase databaseWithPath:filepath];
        
        DLog(@"数据库路径 == %@", filepath);
        
        if ([_fmdb open]) {
            DLog(@"数据库打开成功");
        }
        
        //UserInfoData
        [_fmdb executeUpdate:[NSString stringWithFormat:@"create table if not exists UserInfoData(id integer primary key, guid text, carNo text, name text, car text, deliveryCount number, totalMileage number);"]];
    }
    
    return self;
}

#pragma mark - PKData
- (BOOL)insertUserInfoModel:(UserInfoResultModel *)model
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO UserInfoData(guid, carNo, name, car, deliveryCount, totalMileage) VALUES ('%@', '%@', '%@', '%@', '%@', '%@');",model.guid, model.carNo, model.name, model.car, model.deliveryCount, model.totalMileage];
    BOOL result = [_fmdb executeUpdate:insertSql];
    if (result) {
        DLog(@"插入UserInfoResultModel成功");
    }else {
        DLog(@"插入UserInfoResultModel失败");
    }
    return result;
}

- (NSMutableArray *)queryUserInfoModel:(NSString *)date
{
    NSString *queryString;
    FMResultSet *set;
    
    if (date == nil) {
        queryString = [NSString stringWithFormat:@"SELECT * FROM UserInfoData;"];
        
        set = [_fmdb executeQuery:queryString];
    }else {
        queryString = [NSString stringWithFormat:@"SELECT * FROM UserInfoData where date = ?;"];
        
        set = [_fmdb executeQuery:queryString ,date];
    }
    
    NSMutableArray *arrM = [NSMutableArray array];
    
    while ([set next]) {
        
        NSString *guid = [set stringForColumn:@"guid"];
        NSString *carNo = [set stringForColumn:@"carNo"];
        NSString *car = [set stringForColumn:@"car"];
        NSString *name = [set stringForColumn:@"name"];
        NSNumber *deliveryCount = [NSNumber numberWithInt:[set intForColumn:@"deliveryCount"]];
        NSNumber *totalMileage = [NSNumber numberWithInt:[set intForColumn:@"totalMileage"]];
        
        UserInfoResultModel *model = [[UserInfoResultModel alloc] init];
        
        model.guid = guid;
        model.carNo = carNo;
        model.car = car;
        model.name = name;
        model.deliveryCount = deliveryCount;
        model.totalMileage = totalMileage;
        
        [arrM addObject:model];
    }
    DLog(@"UserInfoData查询成功");
    return arrM;
}

- (BOOL)deleteUserInfoModel:(NSInteger)deleteSql
{
    BOOL result;
    
    if (deleteSql == 4) {
        result =  [_fmdb executeUpdate:@"DELETE FROM UserInfoData"];
    }else {
        NSString *deleteSqlStr = [NSString stringWithFormat:@"DELETE FROM UserInfoData WHERE id = ?"];
        
        result = [_fmdb executeUpdate:deleteSqlStr,[NSNumber numberWithInteger:deleteSql]];
    }
    if (result) {
        DLog(@"删除UserInfoData成功");
    }else {
        DLog(@"删除UserInfoData失败");
    }
    
    return result;
}

@end
